﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;
public partial class admin_ManageUserFeedback : System.Web.UI.Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_USERFEEDBACK = "_UserFeedback::UF";
    private const string PROP_LAYOUT = "_UserFeedback::UIL";
    private const string PROP_SELECTED_USERFEEDBACK_ID = "_UserFeedback::SUFID";

    #endregion

    #region Public Properties

    public UserFeedbackCollection DataSourceUserFeedback
    {
        get { return (SessionMgr.Session[PROP_DATASRC_USERFEEDBACK] is UserFeedbackCollection ? (UserFeedbackCollection)SessionMgr.Session[PROP_DATASRC_USERFEEDBACK] : (UserFeedbackCollection)(SessionMgr.Session[PROP_DATASRC_USERFEEDBACK] = new UserFeedbackCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_USERFEEDBACK] = value; }
    }

    public int SelectedUserFeedbackID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_USERFEEDBACK_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_USERFEEDBACK_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_USERFEEDBACK_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceUserFeedback =UserFeedback.GetCompleteUserFeedback();
            UserFeedbackList1.DataBind(DataSourceUserFeedback);
        }
    }

    #endregion

    #region Control: UserFeedbackList

    protected void ucUserFeedbackList_EditRequested(object sender, Controls_UserFeedbackList.EditRequestedEventArgs evt)
    {
        SelectedUserFeedbackID = evt.UF.UserFeedbackId;
        UserFeedbackViewEdit1.DataBind(evt.UF);
        SetLayout(LayoutType.Edit);
    }


    protected void ucUserFeedbackList_AddNewRequested(object sender, EventArgs evt)
    {
        //UserFeedbackViewEdit1.CancelEdit();
        //UserFeedback newUserFeedback = UserFeedback.CreateNew(DataSourceUserFeedback);
        //SelectedUserFeedbackID = newUserFeedback.UserFeedbackId;
        //UserFeedbackViewEdit1.DataBind(DataSourceUserFeedback.FindByUserFeedbackID(SelectedUserFeedbackID));
        //SetLayout(LayoutType.Edit);
    }


    protected void ucUserFeedbackList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceUserFeedback = UserFeedback.GetCompleteUserFeedback();
        UserFeedbackList1.DataSource = DataSourceUserFeedback;
    }

    #endregion

    #region Control: UserFeedbackViewEdit

    protected void ucUserFeedbackViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        UserFeedbackViewEdit1.DataSourceUserFeedback = DataSourceUserFeedback.FindByUserFeedbackID(SelectedUserFeedbackID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        UserFeedbackViewEdit1.CancelEdit();
        // If we were adding a new UserFeedback...then remove the incomplete/not added new UserFeedback object from the collection.
        if (DataSourceUserFeedback.FindByUserFeedbackID(SelectedUserFeedbackID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceUserFeedback.Remove(DataSourceUserFeedback.FindByUserFeedbackID(SelectedUserFeedbackID));
                SelectedUserFeedbackID = (DataSourceUserFeedback[0] != null ? DataSourceUserFeedback[0].UserFeedbackId : 0);
            }
            catch
            {
            }
        }
        UserFeedbackList1.DataBind(DataSourceUserFeedback);
        SetLayout(LayoutType.View);
    }
    //protected void btnsave_Click(object sender, ImageClickEventArgs e)
    //{

    //    if (UserFeedbackViewEdit1.EndEdit())
    //    {

    //        UserFeedback obj = DataSourceUserFeedback.FindByUserFeedbackID(SelectedUserFeedbackID);
    //        UserFeedback.Update(obj);

    //        // TODO: Set Feedback text...

    //        UserFeedbackList1.DataBind(DataSourceUserFeedback);
    //        SetLayout(LayoutType.View);

    //    }
    //    else
    //    {
    //        divexistbusinesscategoryname.Visible = true;
    //    }
    //}


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlUserFeedbackList.Visible = false;
                pnlUserFeedbackViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlUserFeedbackList.Visible = true;
                pnlUserFeedbackViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion
}
